﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPDemo
{
    public partial class UniversaleMain : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string path = "";
            string path1 = "";

            if (!IsPostBack)
            {
                DataTable dt = AdvertisementManagement.listUniversaleAdvertisement("master", "900100");

                if (dt.Rows.Count > 0)
                {
                    path = dt.Rows[0][6].ToString();
                }
                Advertisement.ImageUrl = path;
            }

            if (Session["username"] != null)
            {
                lblUsername3.Text = Session["username"].ToString();
            }

            if (Session["page"] != null)
            {
                string page = Session["page"].ToString();

                if (page == "register")
                {
                    DataTable dt = AdvertisementManagement.listUniversaleAdvertisement(page, "200400");

                    if (dt.Rows.Count > 0)
                    {
                        path = dt.Rows[0][6].ToString();
                    }
                    Advertisement.ImageUrl = path;
                }

                else if (page == "forum")
                {
                    DataTable dt = AdvertisementManagement.listUniversaleAdvertisement(page, "200400");

                    if (dt.Rows.Count > 0)
                    {
                        path = dt.Rows[0][6].ToString();
                    }
                    Advertisement.ImageUrl = path;
                }

                else if (page == "reward")
                {
                    DataTable dt = AdvertisementManagement.listUniversaleAdvertisement(page, "200400");

                    if (dt.Rows.Count > 0)
                    {
                        path = dt.Rows[0][6].ToString();
                    }
                    Advertisement.ImageUrl = path;
                }

                else if (page == "shopping")
                {
                    DataTable dt = AdvertisementManagement.listUniversaleAdvertisement(page, "200400");

                    if (dt.Rows.Count > 0)
                    {
                        path = dt.Rows[0][6].ToString();
                    }
                    Advertisement.ImageUrl = path;
                }

                else
                {
                    DataTable dt = AdvertisementManagement.listUniversaleAdvertisement("master", "200400");

                    if (dt.Rows.Count > 0)
                    {
                        path = dt.Rows[0][6].ToString();
                    }
                    Advertisement.ImageUrl = path;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            int role = 0;

            string username = null;
            string password = null;
            
            username = txtUsername.Text;
            password = txtPassword.Text;

            role = SecurityManagement.validateMember(username, password);

            if (role != 0)
            {
                Session["Role"] = role;

                string path = Request.Url.AbsoluteUri;

                Session["isLogin"] = "1";
                Session["username"] = username;
                Response.Redirect(path);
            }

            else
            {
                MessageBox.Show("Your username or password had input wrongly. Please try again.");
            }
        }
        
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtSearch.Text;

            if (keyword != "")
            {
                Session["keyword"] = keyword;
                Response.Redirect("SearchList.aspx");
            }
        }

        protected void lbLogout_Click(object sender, EventArgs e)
        {
            Session["isLogin"] = "0";
            Session["VendorCode"] = null;
            Response.Redirect("Home.aspx");
        }

        protected void lbCart_Click(object sender, EventArgs e)
        {
            Response.Redirect("Shoppingcart.aspx");
        }

        protected void lbProfile_Click(object sender, EventArgs e)
        {
            Response.Redirect("partnerPage.aspx");
        }

        protected void lbOrders_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrdersSummary.aspx");
        }
    }
}